package com.cloudfun.campusshare.service.handler;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.KjtAuditStatus;
import com.cloudfun.campusshare.common.constant.SysDictType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.vo.KjtSubMerchantVO;
import com.cloudfun.campusshare.entity.SubMerchantEntity;
import com.cloudfun.campusshare.service.KjtPayeeSelector;
import com.cloudfun.campusshare.service.KjtSubMerchantService;
import com.cloudfun.campusshare.service.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Optional;

/**
 * 默认的快捷通卖家选择器
 * Created by Huqin on 2020/8/11
 */
@Slf4j
@Service
public class DefaultKjtPayeeSelector implements KjtPayeeSelector {

    @Autowired
    private KjtSubMerchantService kjtSubMerchantService;

    @Autowired
    private SysDictService sysDictService;

    @Override
    public String select(String userId) {
        Optional<SubMerchantEntity> entityById = kjtSubMerchantService.getEntityByUserId(userId);
        if (!entityById.isPresent()) {
            throw new BusinessException(Code.ERROR, "联系管理员前往进件快捷通资料");
        }
        SubMerchantEntity subMerchantEntity = entityById.get();
        if (KjtAuditStatus.S != subMerchantEntity.getAuditStatus() || KjtAuditStatus.S != subMerchantEntity.getOpeningStatus()) {
            throw new BusinessException(Code.ERROR, subMerchantEntity.getId() + "子商户的审核状态错误");
        }
        return subMerchantEntity.getIdentityId();
    }
}
